<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        placeholder="搜索内容"
        style="width: 20%;margin-right: 10px;"
        class="filter-item"
        clearable
        @keyup.enter.native="searchClick"
      />

      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="searchClick"
      >搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="addClick"
      >新增
      </el-button>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :row-class-name="tableRowClassName"
      default-expand-all
      :tree-props="{children: 'children'}"
    >
      <el-table-column prop="title" label="菜单名称" />
      <el-table-column prop="path" label="路径" />
      <el-table-column prop="component" label="组件路径" />
      <el-table-column prop="name" label="组件|权限名称" />
      <el-table-column prop="type" label="类型">
        <template slot-scope="scope">
          {{ scope.row.type == 0 ? '平台菜单':'商家菜单' }}
        </template>
      </el-table-column>
      <el-table-column prop="api" label="API权限">
        <template slot-scope="scope">
          {{ scope.row.isApi ? '是':'否' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="editMenuClick(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteMenuClick(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="form.id ? '编辑':'新增' " :visible.sync="menuEditDialogTableVisible">
      <menu-form ref="menuForm" :form="form" :select-menus="selectMenus" @close="close" />
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="menuEditDialogTableVisible = false">取消</el-button>
        <el-button size="small" type="primary" @click="saveMenu">保存</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import request from '@/utils/request'
import MenuForm from './MenuForm'

export default {
  name: 'MenuList',
  components: { MenuForm },
  data() {
    return {
      listQuery: {
        title: ''
      },
      menuEditDialogTableVisible: false,
      tableData: [],
      form: {},
      selectMenus: [],
      allTableData: []
    }
  },
  created() {
    this.getMenus()
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (row.isApi) {
        return 'success-row'
      }
      return ''
    },
    saveMenu: function() {
      this.$refs.menuForm.onSubmit()
    },
    close: function() {
      this.menuEditDialogTableVisible = false
      this.getMenus()
    },
    searchClick: function() {
      this.getMenus()
    },
    editMenuClick: function(row) {
      this.menuEditDialogTableVisible = true
      this.form = this.cloneObject(row)
    },
    addClick: function() {
      this.menuEditDialogTableVisible = true
      this.form = { hidden: false, isApi: false }
    },
    deleteMenuClick: function(row) {
      this.$confirm('确定要删除吗？').then(() => {
        request({
          url: '/api/menu/delete',
          method: 'post',
          data: { id: row.id }
        }).then(res => {
          this.$notify({ message: '操作成功', type: 'success' })
          this.getMenus()
        })
      })
    },
    getSelectMenus: function(menu, all) {
      if (menu == null) {
        all.forEach(v => {
          this.getSelectMenus(v, all)
        })
      } else {
        menu.value = menu.id
        menu.label = '(' + (menu.type == 0 ? '平台菜单' : '商家菜单') + ')' + menu.title
        if (menu.children) {
          menu.children.forEach(v => {
            this.getSelectMenus(v, all)
          })
        }

        // if (children.length > 0 && children[0].children.length == 0) {
        //   menu.children = null
        // }
      }
    },
    getMenus: function() {
      request({
        url: '/api/menu/list',
        method: 'post',
        data: this.listQuery
      }).then(res => {
        this.tableData = res.data
        this.allTableData = this.cloneObject(res.data)
        const menus = this.cloneObject(res.data)
        this.getSelectMenus(null, menus)
        this.selectMenus = menus
      })
    }
  }
}
</script>

<style scoped>

</style>
